﻿
using Arction.Wpf.BindableCharting;
using Arction.Wpf.BindableCharting.SeriesXY;
using Arction.Wpf.BindableCharting.Titles;
using Arction.Wpf.BindableCharting.Views.ViewXY;
using DynamicData;
using Prism.Events;
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

using System.Windows.Input;

using WPF_UI_Lightningchart.ViewModels;
using Binding = System.Windows.Data.Binding;
using ContextMenu = System.Windows.Controls.ContextMenu;
using DragDropEffects = System.Windows.DragDropEffects;
using DragEventArgs = System.Windows.DragEventArgs;
using MenuItem = System.Windows.Controls.MenuItem;
using UserControl = System.Windows.Controls.UserControl;

namespace WPF_UI_Lightningchart.UserControls
{
    /// <summary>
    /// BodeBindableControl.xaml 的交互逻辑
    /// </summary>
    public partial class BodeBindableControl : UserControl
    {
        private readonly BodeBindableControlViewModel _viewModel;

        public BodeBindableControl(IEventAggregator eventAggregator)
        {
            InitializeComponent();
            _viewModel = new BodeBindableControlViewModel(eventAggregator);
            DataContext = _viewModel;
        }

        private void Grid_DragOver(object sender, DragEventArgs e)
        {
            e.Effects = e.Data.GetDataPresent(typeof(SensorViewModel)) ? DragDropEffects.Move : DragDropEffects.None;
            e.Handled = true;
        }

        private void Grid_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(SensorViewModel)))
            {
                var sensorData = e.Data.GetData(typeof(SensorViewModel)) as SensorViewModel;
                _viewModel.OnSeriesAdded(sensorData);
            }
        }


        private void StackPanel_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
        }
        private void OnSettingsMenuItem_Click(object sender, RoutedEventArgs e)
        {

            var menuItem = sender as MenuItem;
            var contextMenu = menuItem?.Parent as ContextMenu;
            var stackPanel = contextMenu?.PlacementTarget as StackPanel;
            var series = stackPanel?.DataContext as PointLineSeries;

            if (series != null)
            {
                var settingsWindow = new SeriesSettingsWindow(series);
                settingsWindow.ShowDialog();
            }
        }
        private void OnDeleteMenuItem_Click(object sender, RoutedEventArgs e)
        {
            var menuItem = sender as MenuItem;
            var contextMenu = menuItem?.Parent as ContextMenu;
            var stackPanel = contextMenu?.PlacementTarget as StackPanel;
            var dataContext = stackPanel?.DataContext;

            if (dataContext is PointLineSeries series)
            {
                _viewModel.AmplitudePointLineSeriesCollection.Remove(_viewModel.AmplitudePointLineSeries[series.Title.Text]);
                _viewModel.AmplitudePointLineSeries.Remove(series.Title.Text);

                _viewModel.PhasePointLineSeriesCollection.Remove(_viewModel.PhasePointLineSeries[series.Title.Text]);
                _viewModel.PhasePointLineSeries.Remove(series.Title.Text);

                _viewModel.RemoveSensorViewModel(series.Title.Text);
            }
        }
    }
}
